
/** cls && adb.exe shell -x "logcat -c && logcat|grep 'MainActivity :'"
*/

/** javac 1.8.0_31 on windows
*/

package demo.apk;

//#region imports
import android.app.Activity;
import android.app.Dialog;
import android.app.NativeActivity;
import android.app.UiModeManager;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.view.SurfaceView;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Logger;
import android.os.Handler;

import android.content.res.Resources;
import android.util.TypedValue;

//#endregion

public class MainActivity extends Activity {
    private final String TAG = "ApkDemo";

    private boolean clicked = false;
    private Button myButton;
    private EditText myEditText;
    private boolean bool_log_input_event = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // // // RelativeLayout rlroot = new RelativeLayout(this);
        // // // rlroot.setBackgroundColor(Color.parseColor("#038fe3")); // Color.BLUE);
        // // // setContentView(rlroot);

        // SurfaceView sv = new SurfaceView(this);
        // sv.setBackgroundColor(Color.parseColor("#038fe3")); // Color.BLUE);
        // setContentView(sv);

        // // desktop_view = new VMDesktopView(this, null, VMDesktopView.MODE_DISPLAY, "Demo"); // desktop_view 不能设置 bc
        // // desktop_view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 0));
        // // desktop_view.setVisibility(View.VISIBLE);
        // // desktop_view.bringup_surface_creation();

        // // setContentView(desktop_view);






        //定义两个控件，button和edittext
        myButton = new Button(this);
        myButton.setText("测试");

        myEditText = new EditText(this);
        myEditText.setHint("测试按钮");

        //为空间设置ID
        myButton.setId(1);
        myEditText.setId(2);

        //定义好父容器并设置相关属性
        RelativeLayout myLayout = new RelativeLayout(this);
        myLayout.setBackgroundColor(Color.parseColor("#038fe3")); // Color.BLUE);

        //设置Button的布局参数
        RelativeLayout.LayoutParams buttonParams =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);

        buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);

        //设置EditText的布局参数
        RelativeLayout.LayoutParams textParams =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);

        textParams.addRule(RelativeLayout.ABOVE, myButton.getId());
        textParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        textParams.setMargins(0, 0, 0, 80);//这里的80是px

        //设置EditText的宽度为指定大小宽度,要相应的dp转化为px
        Resources r = getResources();
        int px = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 200, r.getDisplayMetrics());

        myEditText.setWidth(px);
        //将布局添加到父容器中
        myLayout.addView(myButton, buttonParams);
        myLayout.addView(myEditText, textParams);

        //设置显示父容器
        setContentView(myLayout);

        //为Button设置点击事件，点击使其变宽
        final int wrapWidth = myButton.getLayoutParams().width;
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击Button之后使其宽度变宽，再次点击恢复之前的wrap_content状态，可以配合属性动画使其变化更加自然
                if (!clicked) {
                    myButton.getLayoutParams().width = 800;
                } else {
                    myButton.getLayoutParams().width = wrapWidth;
                }
                clicked = !clicked;
                myButton.requestLayout();
            }
        });

    }

    // 响应触摸屏的事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        {
        int x = (int)(event.getX());
        int y = (int)(event.getY());
        if(bool_log_input_event) Log.i(TAG, "onTouchEvent\t" + MotionEvent.actionToString(event.getActionMasked()) + ";action=" + event.getActionMasked() + ", " + x + "," + y);
        }
        return super.onTouchEvent(event);
    }

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(bool_log_input_event) Log.e(TAG, "键盘按键被按下: " + keyCode);
        return true;
    }


}
